﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data.SqlClient;
namespace canyinguanlixitong
{
    public partial class zhuye : Form
    {
        string constr = "Data Source=.;Initial Catalog=Supermarket management system;Integrated Security=True";
        public zhuye()
        {
            InitializeComponent();
        }
        zengjiaxiaofei zen = new zengjiaxiaofei();

        private void toolStripMenuItem4_Click(object sender, EventArgs e)
        {

        }

        private void groupBox2_Enter(object sender, EventArgs e)
        {

        }
        int p=1;
        private void tabControl2_Selected(object sender, TabControlEventArgs e)
        {
            if (e.TabPage==tabPage4)
            {
                p = 1;
                groupBox1.Text = tabPage4.Text;
            }
            else if (e.TabPage==tabPage5)
            {
                p = 2;
                groupBox1.Text = tabPage5.Text;

            }
            else if (e.TabPage==tabPage6)
            {
                p = 3;
                groupBox1.Text = tabPage6.Text;
            }
        }

        private void groupBox1_Enter(object sender, EventArgs e)
        {

        }

        

        private void zhuye_Load(object sender, EventArgs e)
        {
            
        }
        DataSet ds = new DataSet("cha");
        private void listView1_Click(object sender, EventArgs e)
        {
            groupBox1.Text = string.Empty;
            label8.Text = "不计房间费用";
            SqlConnection sqlcon = new SqlConnection(constr);
            string sql = @"SELECT  Tablecondition.czName, [Table].czID, [Room type].fangJia
FROM      Roombiao INNER JOIN
                   [Room type] ON Roombiao.fangID = [Room type].fangID INNER JOIN
                   [Table] ON Roombiao.fangCz = [Table].czID INNER JOIN
                   Tablecondition ON [Table].cz = Tablecondition.cz";
            sqlcon.Open();
            SqlCommand sqlcom = new SqlCommand(sql, sqlcon);
            SqlDataReader reader = sqlcom.ExecuteReader();
            while (reader.Read())
            {

                if (listView1.SelectedItems[0].Text.Equals(reader["czID"].ToString()))
                {
                    label7.Text = reader["fangJia"].ToString();
                    label12.Text = reader["czName"].ToString();
                    groupBox1.Text = reader["czID"].ToString();
                }
            }
            sqlcon.Close();
            if (ds.Tables["cha"]!=null)
            {
                ds.Tables["cha"].Clear();
            }
            string sql2 = @"SELECT  commodity.spName, commodity.spJia, [Ordering management].dcShul, UserTpye.userName
FROM      commodity INNER JOIN
                   [Ordering management] ON commodity.spID = [Ordering management].dcName INNER JOIN
                   UserTpye ON [Ordering management].dcUserTpye = UserTpye.userID where dcCzid='" + Convert.ToInt32(listView1.SelectedItems[0].SubItems[0].Text) + "' ";
            SqlDataAdapter adapter = new SqlDataAdapter(sql2,sqlcon);
            adapter.Fill(ds,"cha");
            dataGridView1.DataSource=ds.Tables["cha"];
            for (int i = 0; i < dataGridView1.Rows.Count; i++)
            {

                dataGridView1.Rows[i].Cells["dazhebili"].Value = Convert.ToString(1.0);
                dataGridView1.Rows[i].Cells["xiaofeijine"].Value = Convert.ToString(Convert.ToDouble(dataGridView1.Rows[i].Cells["xiangmudanjia"].Value) * Convert.ToDouble(dataGridView1.Rows[i].Cells["dazhebili"].Value) * Convert.ToDouble(dataGridView1.Rows[i].Cells["xiaofeishuliang"].Value));
                
            }
        }

        private void contextMenuStrip1_Opening(object sender, CancelEventArgs e)
        {
           
            for (int i = 0; i < 15; i++)
            {
                contextMenuStrip1.Items[i].Enabled = true;
            }

            if (p==1)
            {
                if (listView1.SelectedItems[0].SubItems[1].Text.Equals("占用"))
                {
                    顾客开单ToolStripMenuItem1.Enabled = false;
                }

                else if (listView1.SelectedItems[0].SubItems[1].Text.Equals("可供"))
                {
                    for (int i = 0; i < 15; i++)
                    {
                        contextMenuStrip1.Items[i].Enabled = false;
                    }
                    顾客开单ToolStripMenuItem1.Enabled = true;
                    顾客预订ToolStripMenuItem.Enabled = true;
                    餐台状态ToolStripMenuItem1.Enabled = true;
                }
            }
            else if (p==2)
            {
                if (listView2.SelectedItems[0].SubItems[1].Text.Equals("占用"))
                {
                    顾客开单ToolStripMenuItem1.Enabled = false;
                }

                else if (listView2.SelectedItems[0].SubItems[1].Text.Equals("可供"))
                {
                    for (int i = 0; i < 15; i++)
                    {
                        contextMenuStrip1.Items[i].Enabled = false;
                    }
                    顾客开单ToolStripMenuItem1.Enabled = true;
                    顾客预订ToolStripMenuItem.Enabled = true;
                    餐台状态ToolStripMenuItem1.Enabled = true;
                }
            }
            else if (p==3)
            {
                if (listView3.SelectedItems[0].SubItems[1].Text.Equals("占用"))
                {
                    顾客开单ToolStripMenuItem1.Enabled = false;
                }

                else if (listView3.SelectedItems[0].SubItems[1].Text.Equals("可供"))
                {
                    for (int i = 0; i < 15; i++)
                    {
                        contextMenuStrip1.Items[i].Enabled = false;
                    }
                    顾客开单ToolStripMenuItem1.Enabled = true;
                    顾客预订ToolStripMenuItem.Enabled = true;
                    餐台状态ToolStripMenuItem1.Enabled = true;
                }
            }
               
                
            
           
        }

        private void 餐台状态ToolStripMenuItem1_Click(object sender, EventArgs e)
        {
            cantaizhuangtai canzuo = new cantaizhuangtai();
            
           
                if (p==1)
                {
                    canzuo.czno = Convert.ToInt32(listView1.SelectedItems[0].SubItems[0].Text);
                    canzuo.Show();
                }
           
           
                if (p==2)
                {
                    canzuo.czno = Convert.ToInt32(listView2.SelectedItems[0].SubItems[0].Text);
                    canzuo.Show();
                }
          
           
    
                if (p==3)
                {
                    canzuo.czno = Convert.ToInt32(listView3.SelectedItems[0].SubItems[0].Text);
                    canzuo.Show();
                }
           
        }

        private void zhuye_Activated(object sender, EventArgs e)
        {
            if (listView1.Items != null)
            {
                listView1.Items.Clear();
            }
            if (listView2.Items != null)
            {
                listView2.Items.Clear();
            }
            if (listView3.Items != null)
            {
                listView3.Items.Clear();
            }
            SqlConnection sqlcon = new SqlConnection(constr);
            sqlcon.Open();
            string sql = @"SELECT  [Table].czID, Tablecondition.czName
FROM      [Table] INNER JOIN
                   Tablecondition ON [Table].cz = Tablecondition.cz";
            SqlCommand sqlcom = new SqlCommand(sql, sqlcon);
            SqlDataReader reader = sqlcom.ExecuteReader();
            int i = 0;
            while (reader.Read())
            {
                ListViewItem li = new ListViewItem();
                if (Convert.ToInt32(reader["czID"]) > 1000 && Convert.ToInt32(reader["czID"]) < 1011)
                {
                    li.SubItems[i].Text = reader["czID"].ToString();
                    if (reader["czName"].ToString().Equals("占用"))
                    {
                        li.ImageIndex = 1;
                    }
                    else if (reader["czName"].ToString().Equals("可供"))
                    {
                        li.ImageIndex = 0;
                    }
                    else if (reader["czName"].ToString().Equals("预订"))
                    {
                        li.ImageIndex = 2;
                    }
                    else if (reader["czName"].ToString().Equals("停用"))
                    {
                        li.ImageIndex = 3;
                    }
                    else if (reader["czName"].ToString().Equals("脏桌"))
                    {
                        li.ImageIndex = 4;
                    }
                    li.SubItems.Add(reader["czName"].ToString());
                    this.listView1.Items.Add(li);
                }
                if (Convert.ToInt32(reader["czID"]) > 2000 && Convert.ToInt32(reader["czID"]) < 2006)
                {
                    li.SubItems[i].Text = reader["czID"].ToString();
                    if (reader["czName"].ToString().Equals("占用"))
                    {
                        li.ImageIndex = 1;
                    }
                    else
                    {
                        li.ImageIndex = 0;
                    }
                    li.SubItems.Add(reader["czName"].ToString());
                    this.listView2.Items.Add(li);
                }
                if (Convert.ToInt32(reader["czID"]) > 3000 && Convert.ToInt32(reader["czID"]) < 3006)
                {
                    li.SubItems[0].Text = reader["czID"].ToString();
                    if (reader["czName"].ToString().Equals("占用"))
                    {
                        li.ImageIndex = 1;
                    }
                    else
                    {
                        li.ImageIndex = 0;
                    }
                    li.SubItems.Add(reader["czName"].ToString());
                    this.listView3.Items.Add(li);
                }

                groupBox1.Text = tabPage4.Text;
            }
            sqlcon.Close();
        }

        private void 顾客开单ToolStripMenuItem1_Click(object sender, EventArgs e)
        {
            顾客开单 kai = new 顾客开单();
            
           
                if (p==1)
                {
                    kai.czno = Convert.ToInt32(listView1.SelectedItems[0].SubItems[0].Text);
                    zen.czno = kai.czno;
                    kai.Show();
                }
           
           
                if (p==2)
                {
                    kai.czno = Convert.ToInt32(listView2.SelectedItems[0].SubItems[0].Text);
                    zen.czno = kai.czno;
                    kai.Show();
                }
           
          
                if (p==3)
                {
                    kai.czno = Convert.ToInt32(listView3.SelectedItems[0].SubItems[0].Text);
                    zen.czno = kai.czno;
                    kai.Show();
                }
           
        }

        private void 增加消费ToolStripMenuItem1_Click(object sender, EventArgs e)
        {
           
                if (p==1)
                {
                    zen.czno = Convert.ToInt32(listView1.SelectedItems[0].SubItems[0].Text);
                    zen.Show();
                }
           
            
                if (p==2)
                {
                    zen.czno = Convert.ToInt32(listView2.SelectedItems[0].SubItems[0].Text);
                    zen.Show();
                }
       
           
                if (p==3)
                {
                    zen.czno = Convert.ToInt32(listView3.SelectedItems[0].SubItems[0].Text);
                    zen.Show();
                }
            
            
        }

        private void 宾客结账ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            bingkejiezhang bing = new bingkejiezhang();
           
                if (p==1)
                {
                    bing.czno = Convert.ToInt32(listView1.SelectedItems[0].SubItems[0].Text);
                    bing.Show();
                }
            
           
                if (p==2)
                {
                    bing.czno = Convert.ToInt32(listView2.SelectedItems[0].SubItems[0].Text);
                    bing.Show();
                }
          
            
                if (p==3)
                {
                    bing.czno = Convert.ToInt32(listView3.SelectedItems[0].SubItems[0].Text);
                    bing.Show();
                }
            
            
           
        }

        private void toolStripButton10_Click(object sender, EventArgs e)
        {
            Form2 form2 = new Form2();
            form2.Show();
        }

        private void toolStripButton9_Click(object sender, EventArgs e)
        {
            Formxitong xitong = new Formxitong();
            xitong.Show();
        }

        private void toolStripButton8_Click(object sender, EventArgs e)
        {
            controlForm contro = new controlForm();
            contro.Show();
        }

        private void toolStripButton7_Click(object sender, EventArgs e)
        {
            frmsyqw3 frmsyQW3 = new frmsyqw3();
            frmsyQW3.Show();
        }

        private void toolStripButton6_Click(object sender, EventArgs e)
        {
            frmsyqw2 frmsyQW2 = new frmsyqw2();
            frmsyQW2.Show();
        }

        private void toolStripButton5_Click(object sender, EventArgs e)
        {
            frmhuiyuan huiyuan = new frmhuiyuan();
            huiyuan.Show();
        }

        private void toolStripButton1_Click(object sender, EventArgs e)
        {
            if (label12.Text.Trim().Equals("可供"))
            {
                顾客开单 kai = new 顾客开单();
                
                    if (p==1)
                    {
                        kai.czno = Convert.ToInt32(listView1.SelectedItems[0].SubItems[0].Text);
                        zen.czno = kai.czno;
                        kai.Show();
                    }
               
                
                    if (p==2)
                    {
                        kai.czno = Convert.ToInt32(listView2.SelectedItems[0].SubItems[0].Text);
                        zen.czno = kai.czno;
                        kai.Show();
                    }
                
                
                    if (p==3)
                    {
                        kai.czno = Convert.ToInt32(listView3.SelectedItems[0].SubItems[0].Text);
                        zen.czno = kai.czno;
                        kai.Show();
                    }
               

            }
        }

        private void toolStripButton11_Click(object sender, EventArgs e)
        {
            if (label12.Text.Trim().Equals("占用"))
            {
                
                    if (p==1)
                    {
                        zen.czno = Convert.ToInt32(listView1.SelectedItems[0].SubItems[0].Text);
                        zen.Show();
                    }
              
             
                    if (p==2)
                    {
                        zen.czno = Convert.ToInt32(listView2.SelectedItems[0].SubItems[0].Text);
                        zen.Show();
                    }
              
                    if (p==3)
                    {
                        zen.czno = Convert.ToInt32(listView3.SelectedItems[0].SubItems[0].Text);
                        zen.Show();
                    }
               

            }
        }

        private void toolStripButton2_Click(object sender, EventArgs e)
        {
            if (label12.Text.Trim().Equals("占用"))
            {
                bingkejiezhang bing = new bingkejiezhang();
               
                    if (p==1)
                    {
                        bing.czno = Convert.ToInt32(listView1.SelectedItems[0].SubItems[0].Text);
                        bing.Show();
                    }
               
                
                    if (p==2)
                    {
                        bing.czno = Convert.ToInt32(listView2.SelectedItems[0].SubItems[0].Text);
                        bing.Show();
                    }
               
              
                    if (p==3)
                    {
                        bing.czno = Convert.ToInt32(listView3.SelectedItems[0].SubItems[0].Text);
                        bing.Show();
                    }
               

            }
        }

        private void toolStripButton3_Click(object sender, EventArgs e)
        {
            kcwm快餐外卖 frm = new kcwm快餐外卖();
            frm.Show();

        }

        private void toolStripButton4_Click(object sender, EventArgs e)
        {
            ydgl预定管理 frm = new ydgl预定管理();
            frm.Show();
        }
        public int cz;
        private void 更换餐台ToolStripMenuItem1_Click(object sender, EventArgs e)
        {
            
            if (p==1)
            {
                panel2.Visible = true;
                cz = Convert.ToInt32(listView1.SelectedItems[0].SubItems[0].Text);
            }
            if (p==2)
            {
                panel3.Visible = true;
                cz = Convert.ToInt32(listView2.SelectedItems[0].SubItems[0].Text);
            }
            if (p==3)
            {
                panel4.Visible = true;
                cz = Convert.ToInt32(listView3.SelectedItems[0].SubItems[0].Text);
            }
            

        }
        
        private void button1_Click(object sender, EventArgs e)
        {
            if (p==1)
            {
                for (int i = 0; i < listView1.Items.Count; i++)
                {
                    //MessageBox.Show(listView1.Items[i].SubItems[0].Text.Trim());

                    if (listView1.Items[i].SubItems[0].Text.Trim().Equals(textBox1.Text.Trim()))
                    {
                        string sql = "select cz from [Table] where czID='" + textBox1.Text.Trim() + "'";
                        SqlConnection sqlcon = new SqlConnection(constr);
                        sqlcon.Open();
                        SqlCommand sqlcom = new SqlCommand(sql, sqlcon);
                        if ((int)sqlcom.ExecuteScalar() == 2)
                        {
                            string sql1 = "update [Ordering management] set dcCzid ='" + textBox1.Text.Trim() + "' where  dcCzid='" + cz + "'";
                            SqlCommand sqlcom1 = new SqlCommand(sql1, sqlcon);
                            string sql2 = @"update [Table] set cz='1' where  czID='" + textBox1.Text.Trim() + @"'
                                        update [Table] set cz='2' where  czID='" + cz + "'";
                            SqlCommand sqlcom2 = new SqlCommand(sql2, sqlcon);
                            if (sqlcom1.ExecuteNonQuery() > 0 && sqlcom2.ExecuteNonQuery() > 0)
                            {
                                MessageBox.Show("更换成功！");
                                panel2.Visible = false;

                                this.Activate();
                            }
                            else
                            {
                                MessageBox.Show("更换失败！");
                            }
                        }
                    }
                }  
            }
            if (p == 2)
            {
                for (int i = 0; i < listView2.Items.Count; i++)
                {
                    //MessageBox.Show(listView2.Items[i].SubItems[0].Text.Trim());

                    if (listView2.Items[i].SubItems[0].Text.Trim().Equals(textBox2.Text.Trim()))
                    {
                        string sql = "select cz from [Table] where czID='" + textBox2.Text.Trim() + "'";
                        SqlConnection sqlcon = new SqlConnection(constr);
                        sqlcon.Open();
                        SqlCommand sqlcom = new SqlCommand(sql, sqlcon);
                        if ((int)sqlcom.ExecuteScalar() == 2)
                        {
                            string sql1 = "update [Ordering management] set dcCzid ='" + textBox2.Text.Trim() + "' where  dcCzid='" + cz + "'";
                            SqlCommand sqlcom1 = new SqlCommand(sql1, sqlcon);
                            string sql2 = @"update [Table] set cz='1' where  czID='" + textBox2.Text.Trim() + @"'
                                        update [Table] set cz='2' where  czID='" + cz + "'";
                            SqlCommand sqlcom2 = new SqlCommand(sql2, sqlcon);
                            if (sqlcom1.ExecuteNonQuery() > 0 && sqlcom2.ExecuteNonQuery() > 0)
                            {
                                MessageBox.Show("更换成功！");
                                panel3.Visible = false;

                                this.Activate();
                            }
                            else
                            {
                                MessageBox.Show("更换失败！");
                            }
                        }
                    }
                }
            }
            if (p == 3)
            {
                for (int i = 0; i < listView3.Items.Count; i++)
                {
                   // MessageBox.Show(listView3.Items[i].SubItems[0].Text.Trim());

                    if (listView3.Items[i].SubItems[0].Text.Trim().Equals(textBox3.Text.Trim()))
                    {
                        string sql = "select cz from [Table] where czID='" + textBox3.Text.Trim() + "'";
                        SqlConnection sqlcon = new SqlConnection(constr);
                        sqlcon.Open();
                        SqlCommand sqlcom = new SqlCommand(sql, sqlcon);
                        if ((int)sqlcom.ExecuteScalar() == 2)
                        {
                            string sql1 = "update [Ordering management] set dcCzid ='" + textBox3.Text.Trim() + "' where  dcCzid='" + cz + "'";
                            SqlCommand sqlcom1 = new SqlCommand(sql1, sqlcon);
                            string sql2 = @"update [Table] set cz='1' where  czID='" + textBox3.Text.Trim() + @"'
                                        update [Table] set cz='2' where  czID='" + cz + "'";
                            SqlCommand sqlcom2 = new SqlCommand(sql2, sqlcon);
                            if (sqlcom1.ExecuteNonQuery() > 0 && sqlcom2.ExecuteNonQuery() > 0)
                            {
                                MessageBox.Show("更换成功！");
                                panel4.Visible = false;

                                this.Activate();
                            }
                            else
                            {
                                MessageBox.Show("更换失败！");
                            }
                        }
                    }
                }
            }
            
        }

        private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {

        }

        private void listView2_Click(object sender, EventArgs e)
        {
            groupBox1.Text = string.Empty;
            label8.Text = "不计房间费用";
            SqlConnection sqlcon = new SqlConnection(constr);
            string sql = @"SELECT  Tablecondition.czName, [Table].czID, [Room type].fangJia
FROM      Roombiao INNER JOIN
                   [Room type] ON Roombiao.fangID = [Room type].fangID INNER JOIN
                   [Table] ON Roombiao.fangCz = [Table].czID INNER JOIN
                   Tablecondition ON [Table].cz = Tablecondition.cz";
            sqlcon.Open();
            SqlCommand sqlcom = new SqlCommand(sql, sqlcon);
            SqlDataReader reader = sqlcom.ExecuteReader();
            while (reader.Read())
            {

                if (listView2.SelectedItems[0].Text.Equals(reader["czID"].ToString()))
                {
                    label7.Text = reader["fangJia"].ToString();
                    label12.Text = reader["czName"].ToString();
                    groupBox1.Text = reader["czID"].ToString();
                }
            }
            sqlcon.Close();
            if (ds.Tables["cha"] != null)
            {
                ds.Tables["cha"].Clear();
            }
            string sql2 = @"SELECT  commodity.spName, commodity.spJia, [Ordering management].dcShul, UserTpye.userName
FROM      commodity INNER JOIN
                   [Ordering management] ON commodity.spID = [Ordering management].dcName INNER JOIN
                   UserTpye ON [Ordering management].dcUserTpye = UserTpye.userID where dcCzid='" + Convert.ToInt32(listView2.SelectedItems[0].SubItems[0].Text) + "' ";
            SqlDataAdapter adapter = new SqlDataAdapter(sql2, sqlcon);
            adapter.Fill(ds, "cha");
            dataGridView1.DataSource = ds.Tables["cha"];
            for (int i = 0; i < dataGridView1.Rows.Count; i++)
            {

                dataGridView1.Rows[i].Cells["dazhebili"].Value = Convert.ToString(1.0);
                dataGridView1.Rows[i].Cells["xiaofeijine"].Value = Convert.ToString(Convert.ToDouble(dataGridView1.Rows[i].Cells["xiangmudanjia"].Value) * Convert.ToDouble(dataGridView1.Rows[i].Cells["dazhebili"].Value) * Convert.ToDouble(dataGridView1.Rows[i].Cells["xiaofeishuliang"].Value));

            }
        }

        private void listView3_Click(object sender, EventArgs e)
        {
            groupBox1.Text = string.Empty;
            label8.Text = "不计房间费用";
            SqlConnection sqlcon = new SqlConnection(constr);
            string sql = @"SELECT  Tablecondition.czName, [Table].czID, [Room type].fangJia
FROM      Roombiao INNER JOIN
                   [Room type] ON Roombiao.fangID = [Room type].fangID INNER JOIN
                   [Table] ON Roombiao.fangCz = [Table].czID INNER JOIN
                   Tablecondition ON [Table].cz = Tablecondition.cz ";
            sqlcon.Open();
            SqlCommand sqlcom = new SqlCommand(sql, sqlcon);
            SqlDataReader reader = sqlcom.ExecuteReader();
            while (reader.Read())
            {
                string str = reader["czID"].ToString();
                
                if (listView3.SelectedItems[0].Text.ToString().Equals(str))
                {
                    label7.Text = reader["fangJia"].ToString();
                    label12.Text = reader["czName"].ToString();
                    groupBox1.Text = reader["czID"].ToString();
                }
            }
            sqlcon.Close();
            if (ds.Tables["cha"] != null)
            {
                ds.Tables["cha"].Clear();
            }
            string sql2 = @"SELECT  commodity.spName, commodity.spJia, [Ordering management].dcShul, UserTpye.userName
FROM      commodity INNER JOIN
                   [Ordering management] ON commodity.spID = [Ordering management].dcName INNER JOIN
                   UserTpye ON [Ordering management].dcUserTpye = UserTpye.userID where dcCzid='" + Convert.ToInt32(listView3.SelectedItems[0].SubItems[0].Text) + "' ";
            SqlDataAdapter adapter = new SqlDataAdapter(sql2, sqlcon);
            adapter.Fill(ds, "cha");
            dataGridView1.DataSource = ds.Tables["cha"];
            for (int i = 0; i < dataGridView1.Rows.Count; i++)
            {

                dataGridView1.Rows[i].Cells["dazhebili"].Value = Convert.ToString(1.0);
                dataGridView1.Rows[i].Cells["xiaofeijine"].Value = Convert.ToString(Convert.ToDouble(dataGridView1.Rows[i].Cells["xiangmudanjia"].Value) * Convert.ToDouble(dataGridView1.Rows[i].Cells["dazhebili"].Value) * Convert.ToDouble(dataGridView1.Rows[i].Cells["xiaofeishuliang"].Value));

            }
        }

        
    }
}
